<?php

namespace IDock\Insurance\AMIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\AMIC\AMICEnums;
use IDock\Insurance\AMIC\Response\AllAMICResponse;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllAMICResponse
{

    /**
     * @var VehicleModelPrinter $printer
     */
    public $printer = VehicleModelPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody()['Body'] ?? [];
        $modelList = $body['vehicleQueryList'] ?? [];
        if (!$modelList) {
            return $this;
        }

        foreach (($modelList['vehicleQuery'] ?? []) as $datum) {

            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $datum['vehicleId'] ?? '';
            $vehiclePrinter->brandName = $datum['vehicleBrand'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['exHaustScale'] ?? '';
            //进口车标识
//            $vehicleType = $datum['countryCode'] == '国产车' ? 'I1' : 'I2';
//            $vehiclePrinter->vehicleImport = $vehicleType;
//            $vehiclePrinter->vehicleImportName = IDockEnums::$importFlag[$vehicleType];
            $vehiclePrinter->vehicleName = $datum['modelName'] ?? '';
            $vehiclePrinter->vehiclePrice = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehiclePriceTax = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['seatCount'] ?? '';
            $vehiclePrinter->familyName = $datum['familyName'] ?? '';
            $vehiclePrinter->remark = $datum['remark'] ?? '';
            $vehiclePrinter->vehicleYear = $datum['caryear'] ? date('Y-m-d',strtotime($datum['caryear'])) : '';
            $tonCount = $datum['tonCount'] ?? 0;
            $vehiclePrinter->vehicleTonnage = $tonCount * 1000;
            $vehiclePrinter->wholeWeight = $datum['emptyWeight'] ? round($datum['emptyWeight'] * 1000,2) : '0';

            //能源类型
            $powerTypeCode = $datum['fuelType'] ?? '0';
            $vehiclePrinter->energyTypeCode =  array_search($powerTypeCode,AMICEnums::$energyType)  ?: 'E00';
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode];
            //新能源
            if (in_array($vehiclePrinter->energyTypeCode,['E02','E03','E04'])) {
                $vehiclePrinter->isNewEnergy = '1';
            }
            $this->printer->setModelList($vehiclePrinter);
        }

        return $this;
    }

    public function filter($vin)
    {
        $printer = new VehicleModelPrinter();
        foreach ($this->printer->toArray() as $datum){
            if(strpos($datum['vehicleName'],$vin) !== false){
                $vehiclePrinter = new VehiclePrinter();
                $vehiclePrinter->vehicleModelCode = $datum['vehicleModelCode'] ?? '';
                $vehiclePrinter->brandName = $datum['brandName'] ?? '';
                $vehiclePrinter->vehicleExhaust = $datum['vehicleExhaust'] ?? '';
                $vehiclePrinter->vehicleName = $datum['vehicleName'] ?? '';
                $vehiclePrinter->vehiclePrice = $datum['vehiclePrice'] ?? '';
                $vehiclePrinter->vehiclePriceTax = $datum['vehiclePriceTax'] ?? '';
                $vehiclePrinter->vehicleSeat = $datum['vehicleSeat'] ?? '';
                $vehiclePrinter->familyName = $datum['familyName'] ?? '';
                $vehiclePrinter->remark = $datum['remark'] ?? '';
                $vehiclePrinter->vehicleYear = $datum['vehicleYear'] ?? '';
                $vehiclePrinter->vehicleTonnage = $datum['vehicleTonnage'] ?? '';
                $vehiclePrinter->wholeWeight = $datum['wholeWeight'] ?? '0';

                //能源类型
                $vehiclePrinter->energyTypeCode =  $datum['energyTypeCode'] ?? '';
                $vehiclePrinter->energyTypeName = $datum['energyTypeName'] ?? '';
                //新能源
                if (in_array($vehiclePrinter->energyTypeCode,['E02','E03','E04'])) {
                    $vehiclePrinter->isNewEnergy = '1';
                }
                $printer->setModelList($vehiclePrinter);
            }
        }
        $this->printer = $printer;
    }
}
